package czopyk.tests;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;

import pl.czopyk.view.QuickDrawer;

public class PerfEsperTest1 {

	static String model = "src/main/resources/models/perf/m2_10u.xml";
	static String controller = "src/main/resources/controllers/perf/controllerEsper{0}.xml";
	private static Map<Integer, String> streamsLabels = new HashMap<Integer, String>();

	static {
		streamsLabels.put(1, "1 strumień");
		streamsLabels.put(4, "4 strumienie");
		streamsLabels.put(8, "8 strumieni");
		streamsLabels.put(12, "12 strumieni");
	}
	
	public static void main(String[] args) throws Exception {
		for (int streams : new Integer[] { 1, 4, 8, 12 }) {
			double result = testMultiple(1, model,
					MessageFormat.format(controller, streams), propietaryClass);
			System.out.println(result);
			QuickDrawer.addXY(streams, result, "Konf. A; Implementacja autorska; 4000 żądań", "");
		}
		QuickDrawer.draw();
	}

	static String propietaryClass = "czopyk.tests.Test1";

	public static double testMultiple(int howMany, String model,
			String controller, String eraClass) throws Exception {
		int i = 0;
		double sum = 0.0;

		String command_new = "\"C:/Program Files/Java/jre1.6.0_20/bin/java.exe\" -Xms128M -Xmx180M -Dfile.encoding=UTF-8 -classpath \"C:/java/workspace/lastAttempt/target/classes;C:/Documents and Settings/czopson_/.m2/repository/jfree/jfreechart/1.0.13/jfreechart-1.0.13.jar;C:/Documents and Settings/czopson_/.m2/repository/jfree/jcommon/1.0.15/jcommon-1.0.15.jar;C:/Documents and Settings/czopson_/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar;C:/Documents and Settings/czopson_/.m2/repository/sax/sax/2.0.1/sax-2.0.1.jar;C:/Documents and Settings/czopson_/.m2/repository/colt/colt/1.2.0/colt-1.2.0.jar;C:/Documents and Settings/czopson_/.m2/repository/concurrent/concurrent/1.3.4/concurrent-1.3.4.jar;C:/Documents and Settings/czopson_/.m2/repository/commons-lang/commons-lang/2.5/commons-lang-2.5.jar;C:/Documents and Settings/czopson_/.m2/repository/com/espertech/esper/4.0.0/esper-4.0.0.jar;C:/Documents and Settings/czopson_/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar;C:/Documents and Settings/czopson_/.m2/repository/org/antlr/antlr-runtime/3.2/antlr-runtime-3.2.jar;C:/Documents and Settings/czopson_/.m2/repository/org/antlr/stringtemplate/3.2/stringtemplate-3.2.jar;C:/Documents and Settings/czopson_/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar;C:/Documents and Settings/czopson_/.m2/repository/cglib/cglib-nodep/2.2/cglib-nodep-2.2.jar;C:/Documents and Settings/czopson_/.m2/repository/com/google/guava/guava/r07/guava-r07.jar;C:/java/workspace/Drawer/target/classes;C:/java/workspace/MathExtension/target/classes\" czopyk.tests.Test1";

		String command = command_new + " " + model + " " + controller;
		while (i++ < howMany) {
			double t = System.currentTimeMillis();
			System.err.println("executing command: " + command);
			Process p = Runtime.getRuntime().exec(command);
			 IOUtils.copy(p.getInputStream(), System.out);
			//IOUtils.copy(p.getInputStream(), new NullOutputStream());
			//IOUtils.copy(p.getErrorStream(), new NullOutputStream());
			 IOUtils.copy(p.getErrorStream(), System.err);
			p.waitFor();
			t = System.currentTimeMillis() - t;
			sum += t;
		}
		return ((sum / 1000.0) / howMany);
	}
}